import axios from 'axios';
axios.defaults.baseURL = 'http://47.90.62.38:9091/code_main';
axios.defaults.baseURL = 'http://localhost:9090/code_main'
axios.defaults.withCredentials = true;

// localStorage.setItem('TOKEN', token)

axios.interceptors.request.use(
    config =>{

        const {url} = config
        let token = localStorage.getItem('TOKEN');

        /**

        if(/^\/api\//.test(url) && !token && !window.location.href.indexOf('user') > -1){
            const { dispatch } = store
            dispatch(routerRedux.replace('/user/login')) // 跳转到登录页
        }
        */

        if(token && url != '/api/login') {
            config.headers.Authorization = 'Bearer ' + token // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
            /*
            config.params = {
                token: token,
                ...config.params
            }*/
        }
        return config;
    }, error => {
        console.info('---------------------'+error)
        // return Promise.reject(err);

    }
)


axios.interceptors.response.use(
    res =>{

        // console.info(JSON.stringify(res) + '=======')
        return res;
    },
    error =>{
        // 重新登录
        location.replace('#/login');
        return Promise.reject(error.response)
    })


export default axios;
